package br.com.base.business.impl.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UtilData {

	public static final String PATTNER_YYYY_MM_DD = "yyyy-MM-dd";

	public static Date converterStringToDate(String data){
		Date date = null;
		try{
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(data);
		}catch(Exception e){

		}
		return date;
	}

	public static String getDataDateHorasLongToString(Date data, long hora){
		StringBuilder dataHota = new StringBuilder();
		if(data != null){
			dataHota.append(getDateString(data));
			dataHota.append(" ");
			dataHota.append(getSegundosToHoras(hora));
		}
		return dataHota.toString();
	}

	public static String getDataDateHorasLongToStringBr(Date data, long hora){
		StringBuilder dataHota = new StringBuilder();
		if((data != null) && (hora > 0)){
			dataHota.append(getSegundosToHoras(hora));
			dataHota.append(" ");
			dataHota.append(getDateStringPtBr(data));
		}
		return dataHota.toString();
	}

	public static String getDataDateHorasSegundosLongToString(Date data, long hora){
		StringBuilder dataHota = new StringBuilder();
		if(data != null){
			dataHota.append(getDateString(data));
			dataHota.append(" ");
			dataHota.append(getSegundosToHorasSegundos(hora));
		}
		return dataHota.toString();
	}

	public static String getDateString(Date date){
		return getDateString(date, "yyyy-MM-dd");
	}

	public static String getDateString(Date date, String padrao){
		String dateString = "";
		try{
			SimpleDateFormat format = new SimpleDateFormat(padrao);
			dateString = format.format(date);
		}catch(Exception e){
			e.printStackTrace();
		}
		return dateString;
	}

	public static String getDateStringPtBr(Date date){
		return getDateString(date, "dd/MM/yyyy");
	}

	public static String getSegundosToHoras(Integer segundos){
		String format = "";
		if(segundos != null){
			format = getSegundosToHoras(Long.parseLong(String.valueOf(segundos)));
		}
		return format;
	}

	public static String getSegundosToHoras(Long segundos){
		StringBuilder hora = new StringBuilder();
		if(segundos != null){
			long min = segundos / 60;
			int horas = (int) (min / 60);
			int minHora = (int) (min % 60);

			hora.append(horas);
			hora.append(":");
			if(minHora < 10){
				hora.append("0");
			}
			hora.append(minHora);
		}
		return hora.toString();
	}

	public static String getSegundosToHorasSegundos(Long segundos){
		StringBuilder hora = new StringBuilder();
		if(segundos != null){
			long min = segundos / 60;
			int horas = (int) (min / 60);
			int segundosRt = (int) (min / 120);

			int minHora = (int) (min % 60);


			hora.append(horas);
			hora.append(":");
			if(minHora < 10){
				hora.append("0");
			}

			hora.append(minHora);
			hora.append(":");
			if(segundosRt < 10){
				hora.append("0");
			}
			hora.append(segundosRt);

		}
		return hora.toString();
	}


	public static Long getTempoSegundoDate(Date date){
		long horaSeg = 0L;
		try{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String dateString = format.format(date);
			Date dataIni = format.parse(dateString);
			horaSeg = (long) ((date.getTime() - dataIni.getTime()) * 0.001);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return horaSeg;
	}

	public static void main(String[] args) {
		System.out.println(getDataDateHorasSegundosLongToString(new Date(System.currentTimeMillis()), 86331L));
	}

	public String getDataAtual(){
		return new SimpleDateFormat("dd/MM/yyyy").format(new Date(System.currentTimeMillis()));
	}

}
